home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Palettes / Clocks / AnalogClock.m < prev    next >
Text File  |  1995-06-12  |  5KB  |  201 lines

  1. //----------------------------------------------------------------------------------------------------
  2. //
  3. //    AnalogClock
  4. //
  5. //    Inherits From:        Clock
  6. //
  7. //    Declared In:        AnalogClock.h
  8. //
  9. //    Disclaimer
  10. //
  11. //        You may freely copy, distribute and reuse this software and its
  12. //        associated documentation. I disclaim any warranty of any kind, 
  13. //        expressed or implied, as to its fitness for any particular use.
  14. //
  15. //----------------------------------------------------------------------------------------------------
  16. #import "AnalogClock.h"
  17. #import <math.h>
  18.  
  19.  
  20. #define HOUR_HAND_LENGTH             15.0
  21. #define MINUTE_HAND_LENGTH         22.0
  22. #define SECOND_HAND_LENGTH        24.0
  23.  
  24. #define HOUR_HAND_LINE_WIDTH         2.0
  25. #define MINUTE_HAND_LINE_WIDTH     2.0
  26. #define SECOND_HAND_LINE_WIDTH    0.0
  27.  
  28.  
  29. @implementation AnalogClock
  30.  
  31. //----------------------------------------------------------------------------------------------------
  32. //    Initialization and Freeing
  33. //----------------------------------------------------------------------------------------------------
  34. - initFrame: (const NXRect *)aRect
  35. {
  36.     //  Give default characteristics.  Fix size to 64 x 64. 
  37.     
  38.     NXRect    frameRect;
  39.     NXSetRect (&frameRect, NX_X(aRect), NX_Y(aRect), 64.0, 64.0);
  40.     [super initFrame:&frameRect];
  41.     
  42.     [self  hourAndMinuteHandColor: NX_COLORBLACK];
  43.     [self  secondHandColor: NX_COLORRED];
  44.     
  45.     [self clockFace: [NXImage findImageNamed: "AnalogFaceMarble"]];
  46.     [self wantsDate: NO];
  47.     
  48.     return self;
  49. }
  50.  
  51.  
  52. //----------------------------------------------------------------------------------------------------
  53. //    Accessing Clock Hand Colors
  54. //----------------------------------------------------------------------------------------------------
  55. - hourAndMinuteHandColor: (NXColor) aColor
  56. {
  57.     hourAndMinuteHandColor = aColor;
  58.     [self update];
  59.     return self;
  60. }
  61.  
  62.  
  63. - secondHandColor: (NXColor) aColor
  64. {
  65.     secondHandColor = aColor;
  66.     [self update];
  67.     return self;
  68. }
  69.  
  70.  
  71. - (NXColor) hourAndMinuteHandColor
  72. {
  73.     return hourAndMinuteHandColor;
  74. }
  75.  
  76.  
  77. - (NXColor) secondHandColor
  78. {
  79.     return secondHandColor;
  80. }
  81.  
  82.  
  83. //----------------------------------------------------------------------------------------------------
  84. //    Action Methods
  85. //----------------------------------------------------------------------------------------------------
  86. - takeHourAndMinuteHandColorFrom: sender
  87. {
  88.     if ([sender respondsTo: @selector (color)]) [self hourAndMinuteHandColor: [sender color]];
  89.     return self;
  90. }
  91.  
  92.  
  93. - takeSecondHandColorFrom: sender
  94. {
  95.     if ([sender respondsTo: @selector (color)]) [self secondHandColor: [sender color]];
  96.     return self;
  97. }
  98.  
  99.  
  100. //----------------------------------------------------------------------------------------------------
  101. //    Draw Methods
  102. //-----------------------------------------------------------------------------------------------------
  103. - drawTime
  104. {
  105.     float  secondHandAngle = (float) (SECOND(displayTimeAndDate) * 6 * M_PI / 180.0);
  106.     float  minuteHandAngle = (float) (MINUTE(displayTimeAndDate) * 6 * M_PI / 180.0);
  107.     float  hourHandAngle = ((HOUR(displayTimeAndDate)) %12) * 30 * M_PI / 180.0 +         minuteHandAngle / 12;
  108.  
  109.     PStranslate (NX_MIDX(&bounds), NX_MIDY(&bounds));
  110.  
  111.     //  second hand
  112.     if (flags.wantsSeconds)
  113.         {
  114.         PSsetlinewidth (SECOND_HAND_LINE_WIDTH);
  115.         NXSetColor (secondHandColor);
  116.         PSmoveto (0.0,0.0);
  117.         PSlineto (sin (secondHandAngle) * SECOND_HAND_LENGTH, 
  118.             cos (secondHandAngle) * SECOND_HAND_LENGTH);
  119.         PSstroke ();
  120.         }
  121.  
  122.     NXSetColor (hourAndMinuteHandColor);
  123.     
  124.     //  minute hand
  125.     PSsetlinewidth (MINUTE_HAND_LINE_WIDTH);
  126.     PSmoveto (sin (minuteHandAngle) * MINUTE_HAND_LENGTH, 
  127.          cos (minuteHandAngle) * MINUTE_HAND_LENGTH);
  128.     PSlineto (0.0, 0.0);
  129.     PSstroke ();
  130.  
  131.     //  hour hand
  132.     PSsetlinewidth (HOUR_HAND_LINE_WIDTH);
  133.     PSmoveto (0.0, 0.0);
  134.     PSlineto (sin (hourHandAngle) * HOUR_HAND_LENGTH, 
  135.         cos (hourHandAngle) * HOUR_HAND_LENGTH);
  136.     PSstroke ();
  137.     PStranslate (-(NX_MIDX(&bounds)), -(NX_MIDY(&bounds)));
  138.         
  139.     return self;
  140. }
  141.  
  142.  
  143. //----------------------------------------------------------------------------------------------------
  144. //    Archiving Methods
  145. //----------------------------------------------------------------------------------------------------
  146. - read: (NXTypedStream*) stream
  147. {
  148.     [super read: stream];
  149.     hourAndMinuteHandColor =  NXReadColor (stream);
  150.     secondHandColor = NXReadColor (stream);
  151.     return self;
  152. }
  153.  
  154.  
  155. - write: (NXTypedStream*) stream
  156. {
  157.     [super write: stream];
  158.     NXWriteColor (stream, hourAndMinuteHandColor);
  159.     NXWriteColor (stream, secondHandColor);
  160.     return self;
  161. }
  162.  
  163.  
  164. //----------------------------------------------------------------------------------------------------
  165. //    Superclass Overrides
  166. //----------------------------------------------------------------------------------------------------
  167. - wantsMilitaryTime: (BOOL) aFlag
  168. {
  169.     return [super wantsMilitaryTime: NO];
  170. }
  171.  
  172.  
  173. - wantsDate: (BOOL) aFlag
  174. {
  175.     return [super wantsDate: NO];
  176. }
  177.  
  178.  
  179. //----------------------------------------------------------------------------------------------------
  180. //    IB Methods
  181. //----------------------------------------------------------------------------------------------------
  182. - getMinSize:(NXSize *)minSize maxSize:(NXSize *)maxSize from:(int)where
  183. {
  184.     //  Constrains resize within IB.
  185.     
  186.     minSize->width = maxSize->width = 64.0;
  187.     minSize->height = maxSize->height = 64.0;
  188.     return self;
  189. }
  190.  
  191.  
  192. - (const char*) getInspectorClassName
  193. {
  194.     //  Returns the name of the class responsible for managing custom 
  195.     //  IB Attributes inspection.
  196.         
  197.     return "AnalogClockInspector";
  198. }
  199.  
  200.  
  201. @end